package edu.sdust.ssmstudy.service;

import com.github.pagehelper.PageInfo;

import edu.sdust.ssmstudy.dao.JobsMapper;
import edu.sdust.ssmstudy.json.BaseJson;
import edu.sdust.ssmstudy.json.PaInfo;
import edu.sdust.ssmstudy.json.SelectJson;
import edu.sdust.ssmstudy.json.base.Condition;
import edu.sdust.ssmstudy.json.base.Page;
import edu.sdust.ssmstudy.json.base.Select;
import edu.sdust.ssmstudy.json.base.Sort;
import edu.sdust.ssmstudy.pojo.City;
import edu.sdust.ssmstudy.pojo.Jobs;
import edu.sdust.ssmstudy.pojo.Money;
import edu.sdust.ssmstudy.pojo.Trades;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;


import java.util.List;

@Service
public class JobServiceImpl implements JobService {
    @Autowired
    private JobsMapper jobMapper;


    public List<Jobs> SelectAll() {
        List<Jobs> jobs = jobMapper.selectAll();
        return jobs;
    }

    public List<Jobs> SelectByCity(String city) {
        List<Jobs> jobs = jobMapper.SelectByCity(city);
        return jobs;
    }


    public Jobs SelectById(Integer id) {
        Jobs job = jobMapper.selectByPrimaryKey(id);
        return job;
    }
    public List<Jobs> GetTopTen(){
        List<Jobs> Top = jobMapper.GetmaxofferTop();
        return Top;
    }
    public List<String> SelectDifCity(){
        List<String> city = jobMapper.SelectDifCity();
        return city;
    }

    public Integer CityCount(String city) {
        int count = jobMapper.CityCount(city);
        return count;
    }


    public Integer GetExCount(String work_experience) {
        Integer count = jobMapper.GetExCount(work_experience);
        return count;
    }

    public List<String> GetDifEx() {
        List<String> experience = jobMapper.SelectDifEx();
        return experience;
    }

    public PageInfo<Jobs> findAll(BaseJson baseJson) {
        Sort sort = baseJson.getSort();
        Page page = baseJson.getPage();

        //example 中可以包括查询的条件信息(where 语句)以及排序信息(sort by语句)
        Example example = new Example(Jobs.class);
        if(sort != null)
            example.setOrderByClause(sort.getFormatString());

        //提交查询时，大多数情况需要分页，此时调用你selectByExampleAndRowBounds()方法进行查询
        //若无需分页，调用selectByExample进行查询(此时无需RowBounds参数)。
        List<Jobs> jobs = jobMapper.selectByExampleAndRowBounds(example, page.getRowBounds());


        //查询结果是一个list，使用new PageInfo可以变为一个前台需要的PageInfo对象（包含分页信息和数据）
        return new PageInfo<Jobs>(jobs);
    }

    public PageInfo<Jobs> find(SelectJson selectJson) {
        //查询的条件信息。包括条件List以及条件匹配方式（全部符合或者任一符合）
        Select select = selectJson.getSelect();
        List<Condition> conditionList = select.getConditionList();
        String method = select.getMethod();

        //分页和排序信息
        Sort sort = selectJson.getSort();
        Page page = selectJson.getPage();

        //example 中可以包括查询的条件信息(where 语句)以及排序信息(sort by语句)，排序最好放在最后写
        Example example = new Example(Jobs.class);
        if(method == null)
            throw new RuntimeException("内部错误:查询方法为空");
        else if(method.equals("all")){
            Example.Criteria criteria = example.createCriteria();
            for(Condition condition:select.getConditionList()){
                criteria.andCondition(condition.getCondition(),condition.getValue());
            }
        }else if(method.equals("any")){
            Example.Criteria criteria = example.createCriteria();
            for(Condition condition:select.getConditionList()){
                criteria.orCondition(condition.getCondition(),condition.getValue());
            }
        }else{
            throw new RuntimeException("内部错误:无法识别的查询方法:" + method);
        }

        example.setOrderByClause(sort.getFormatString());

        List<Jobs> jobs = jobMapper.selectByExampleAndRowBounds(example, page.getRowBounds());
        return new PageInfo<Jobs>(jobs);
    }

    public List<Trades> GetTopCityTrades(String title) {
        List<Trades> city = jobMapper.GetTopCityTrades(title);
        return city;
    }

    public List<Trades> GetCityCount() {
        List<Trades> city = jobMapper.GetCityCount();
        return city;
    }

    public List<Trades> GetWxCount(String title) {
        List<Trades> wx = jobMapper.GetWxCount(title);
        return wx;
    }


    public List<Jobs> GetPageInfo(PaInfo pageInfo) {
        //pageInfo = new PaInfo(1,10,1);
        List<Jobs> jobs = jobMapper.GetPageInfo(pageInfo);
        return jobs;
    }

    public List<Trades> GetTradesHot(String title) {
        List<Trades> thot = jobMapper.GetTradesHot(title);
        return thot;
    }

    public List<Money> GetMaxTop(String title){
        List<Money> mt = jobMapper.GetMaxTop(title);
        return mt;
    }

    public Trades TradesInfo(String trades) {
        Trades tra = new Trades(trades,Long.valueOf(jobMapper.TradesInfo(trades)));
        return tra;
    }

    public Trades TitleInfo(String title) {
        Trades tra = new Trades(title,Long.valueOf(jobMapper.TitleInfo(title)));
        return tra;
    }

}